package com.zbs.test.work.work02;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.zbs.test.utils.RabbitMqUtil;
import com.zbs.test.utils.ThreadUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class Work02 {
    final static String queueName = "ack_test";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtil.getChannel();

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            ThreadUtils.sleep(1000*10);
            /**
             * 1 消息标记 2是否批量
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
            System.out.println("接收到的消息："+ new String(message.getBody(), StandardCharsets.UTF_8));
        };
        CancelCallback cancelCallback = (String consumerTag) -> {
            System.out.println("消息者取消消息："+consumerTag);
        };
        /*
          接收消息
          1 队列名称
          2 消费成功后是否自动应答
          3 消费者未成功消费回调
          4 消费者取消消费的回调
         */
        channel.basicQos(1); // 设置不公平分发
        System.out.println("C2等待接收消息，时间较长....");
        boolean autoAck = false;
        channel.basicConsume(queueName,autoAck,deliverCallback,cancelCallback);
    }
}
